package com.wushi.lenovo.asia5b.app_di.credit.presenter;

import android.text.TextUtils;

import com.mvp.base.BasePresenter;
import com.wushi.lenovo.asia5b.app_di.credit.mvpview.CardFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.TopupService;
import com.wushi.lenovo.asia5b.app_model.credit.bean.ThirdTopupType;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;


/**
 * Created by zhangyuncai on 2019/1/20.
 */
public class CardFragmentPresenter extends BasePresenter<CardFragmentView> {

    ApiService apiService;
    TopupService topupService;

    @Inject
    public CardFragmentPresenter(ApiService apiService, TopupService topupService) {
        this.apiService = apiService;
        this.topupService = topupService;
    }

    public void validateTopup(Map<String, Object> map, ThirdTopupType thirdTopupType) {
        baseSubscribe(topupService.validateTopup(thirdTopupType.getValidateTopupPath(), map),
                resultModel -> {
                    getView().validateTopupResult(resultModel, map);
                });
    }

    //Visa Mastercard
    public String getNumberCardType(String card_number) {
        if (TextUtils.isEmpty(card_number)) {
            return null;
        }
        Pattern p = Pattern.compile("^([4]{1})([0-9]{12,15})$");
        Matcher m = p.matcher(card_number);
        if (m.matches()) {
            return "Visa";
        } else {
            p = Pattern.compile("^([5]{1})([0-9]{15})$");
            m = p.matcher(card_number);
            if (m.matches()) {
                return "Mastercard";
            } else {
                return null;
            }
        }
    }
}